package com.trkj.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.CcBerthDAO;
import com.trkj.dao.jpa.CcDeductDAO;
import com.trkj.dao.jpa.CcDeductdetailDAO;
import com.trkj.dao.jpa.CcPatientDAO;
import com.trkj.dao.jpa.CcPrepaymentDAO;
import com.trkj.dao.jpa.CcWardDAO;
import com.trkj.pojos.CcBerth;
import com.trkj.pojos.CcDeduct;
import com.trkj.pojos.CcDeductdetail;
import com.trkj.pojos.CcPatient;
import com.trkj.pojos.CcPrepayment;
import com.trkj.pojos.CcWard;
import com.trkj.vo.CcPrepaymentVo;

@Service
@Transactional
/**
 * 预交费
 * @author Administrator
 *
 */
public class CcPrepaymentService {

	@Autowired
	private CcPrepaymentDAO ccPrepaymentDAO;
	@Autowired
	private CcDeductDAO cddDAO;
	@Autowired
	private CcDeductdetailDAO cdddDAO;
	@Autowired
	private CcPatientDAO cpDAO;
	@Autowired
	private CcBerthDAO cbDAO;
	@Autowired
	private CcWardDAO cwDAO;
	public void addCcPrepayment(CcPrepayment ccPrepayment){
		ccPrepaymentDAO.save(ccPrepayment);
	}
	public  List<CcPrepaymentVo>  queryByOne(String icid){
		CcPatient cp= cpDAO.findByOneCcPatient(new BigDecimal(icid));
		
		if(cp!=null){
			CcPrepayment cc=ccPrepaymentDAO.findBypatient(new BigDecimal(cp.getPatientid()));
			List<CcPrepayment> list=new ArrayList<>();
			list.add(cc);
			return ccPrepaymentTOCcPrepaymentVo(list);
		}else{
			return null;
		}
	}
	public void updatePpremark(){
		List<CcPrepayment> list=ccPrepaymentDAO.findByppremarks1();
		for(CcPrepayment cpm:list){
			CcPatient cp=cpDAO.findByOneCcPatientid(cpm.getPpatientno().longValue());
			if(cp !=null &&cp.getPremarks()!=null&&cp.getPremarks().equals("已入住")){
				CcBerth cb= cbDAO.findOne(cp.getPaberthid().longValue());
				CcWard cw=cwDAO.findOne(cb.getBwardno().longValue());
				CcPrepayment cpp=ccPrepaymentDAO.findBypatient(cb.getBpId());
				cpp.setPpatientno(cpm.getPpatientno());
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				String dateString = formatter.format(new Date());
				String pp=cpp.getPpremarks();
				System.err.println(pp+"---------pp");
				System.err.println(dateString+"------------dateString");
				String[] jg=pp.split("-");
				String[] jg2=dateString.split("-");
				if(jg[0].equals(jg2[0])){
					if(jg[1].equals(jg2[1])){
						if(!jg[2].equals(jg2[2])){
							System.err.println(jg2[2]+"------2");
							System.err.println(jg[2]+"------1");
							int i=Integer.parseInt(jg[2]);
							int y=Integer.parseInt(jg2[2]);
							if(y>i){
								int j=y-i;
								cpp.setPpmoney(cpm.getPpmoney().subtract(cw.getWmoney().multiply(new BigDecimal(j))));
								cpp.setPpremarks(dateString);
								ccPrepaymentDAO.save(cpp);
								for(int k=0;k<j;k++){
									CcDeduct cd=new CcDeduct();
									cd.setDmoney(cw.getWmoney());
									cd.setDpaymentid(new BigDecimal(cpm.getPpaymentid()));
									cd.setDedate(new Date());
									cddDAO.save(cd);
									
									CcDeductdetail cdd=new CcDeductdetail();
									cdd.setDdeductid(new BigDecimal(cd.getDeductid()));
									cdd.setDdmoney(cd.getDmoney());
									cdd.setDdname("床位费");
									cdd.setDddate(new Date());
									cdddDAO.save(cdd);
								}
							}
						}
						//-------
					}
				}
			}
		}
	}
	public Map<String,Object> queryByppmoney(int page,int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<CcPrepayment> list=ccPrepaymentDAO.findByppmoney(pr, new BigDecimal(500));
		Map<String, Object> map = new HashMap<>();
		map.put("total", ccPrepaymentDAO.countByppmoney(new BigDecimal(500)));
		map.put("rows", ccPrepaymentTOCcPrepaymentVo(list));
		return map;
	}
	public List<CcPrepaymentVo> ccPrepaymentTOCcPrepaymentVo(List<CcPrepayment> list){
		List<CcPrepaymentVo> listVo=new ArrayList<CcPrepaymentVo>();
		for(CcPrepayment cpp:list){
			CcPatient cp=cpDAO.findOne(cpp.getPpatientno().longValue());
			if(cp!=null&&cp.getPaberthid()!=null&&!cp.getPremarks().equals("已出院")){
				CcPrepaymentVo cppVo=new CcPrepaymentVo();
				cppVo.setPpremarks(cpp.getPpremarks());
				cppVo.setPpaymentid(cpp.getPpaymentid());
				cppVo.setPpatientno(cpp.getPpatientno());
				cppVo.setPpmoney(cpp.getPpmoney());
				cppVo.setBerthid(cp.getPaberthid().longValue());
				cppVo.setPname(cp.getPname());
				cppVo.setIcid(cp.getIcid());
				CcBerth cb= cbDAO.findOne(cppVo.getBerthid());
				cppVo.setBname(cb.getBname());
				cppVo.setWardid(cb.getBwardno().longValue());
				CcWard cw=cwDAO.findOne(cppVo.getWardid());
				cppVo.setWname(cw.getWname());
				listVo.add(cppVo);
			}
		}
		return listVo;
	}
}
